package com.itheima.tanhua.server.controller;


import cn.hutool.core.date.DateTime;
import com.itheima.commons.utils.JwtUtils;
import com.itheima.commons.utils.OssUtils;
import com.itheima.tanhua.dubbo.api.IUseLoginSevice;
import com.itheima.tanhua.model.domain.User;
import com.itheima.tanhua.model.domain.bto.LoginReginfobto;
import com.itheima.tanhua.model.domain.bto.LoginVerificationbto;
import com.itheima.tanhua.model.domain.vo.LoginVerificationVo;
import io.jsonwebtoken.Claims;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;

/**
 * @author LENOVO
 */
@RestController
@RequestMapping("user")
public class UserLoginController {

    @DubboReference
    private IUseLoginSevice userLoginService;

    @Autowired
    private RedisTemplate redisTemplate;

    public UserLoginController() {
    }

    /**
     * 发送验证码
     *
     * @param bto
     */
    @PostMapping("/login")
    public void login(@RequestBody LoginVerificationbto bto) {
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 4; i++) {
            int i1 = random.nextInt(4);
            code += i1;
        }
        redisTemplate.opsForValue().set("code", code);
        System.out.println(code);

    }


    /**
     * 校验，返回token
     *
     * @param bto
     * @return
     * @throws Exception
     */
    @PostMapping("/loginVerification")
    public LoginVerificationVo loginVerification(@RequestBody LoginVerificationbto bto) throws Exception {
        if (!redisTemplate.opsForValue().get("code").equals(bto.getVerificationCode())) {
            throw new Exception("验证码有误");
        }
        String token = null;
        LoginVerificationVo vo = new LoginVerificationVo();
        User login = userLoginService.login(bto.getPhone());
        if (login != null) {
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            Long id = login.getId();

            objectObjectHashMap.put("id", id);
            objectObjectHashMap.put("phone", bto.getPhone());

            token = JwtUtils.getToken(objectObjectHashMap);
            System.out.println(token);
            redisTemplate.opsForValue().set("id", id);

            vo.setToken(token);
            vo.setIsNew("false");

            return vo;
        } else {
            User user = new User();
            user.setHxPassword("123456");
            user.setMobile(bto.getPhone());

            redisTemplate.opsForValue().set("phone", bto.getPhone());
//            LocalDateTime now = LocalDateTime.now();
            user.setCreated(DateTime.now());
//            user.setCreated(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()));
            userLoginService.addUser(user);

            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("phone", bto.getPhone());

            token = JwtUtils.getToken(objectObjectHashMap);

            vo.setToken(token);
            vo.setIsNew("true");

            return vo;
        }


    }

    @PostMapping("/loginReginfo")
    public void loginReginfo(@RequestBody LoginReginfobto loginReginfobto, @RequestHeader String authorization) {
        System.out.println(loginReginfobto.getCity());
        //token校验
        Claims claims = JwtUtils.getClaims(authorization);
        loginReginfobto.setCreated(DateTime.now());
//        获取手机号码
        String phone = (String) redisTemplate.opsForValue().get("phone");
        userLoginService.addUserMsg(loginReginfobto, phone);

    }

    @PostMapping("/loginReginfo/head")
    public void head(MultipartFile headPhoto,@RequestHeader String authorization ) throws IOException {
        Claims claims = JwtUtils.getClaims(authorization);
        Long id = Long.valueOf(claims.get("id").toString());

        OssUtils ossUtils = new OssUtils();
        String url = ossUtils.ossupload(headPhoto);

        userLoginService.addUserMsgPath(url, id);
    }

    @GetMapping
    public void user(){

    }

}
